package com.demo.user.util.page;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;

import java.util.Collections;
import java.util.List;

/**
 * @author xiexiaohu
 */
@Data
@ToString
@ApiModel(value = "分页响应数据")
public class BasePaginationRsp<T> {
    private static final long serialVersionUID = 6886729754989778218L;

    /**
     * 总数.
     */
    @ApiModelProperty(value = "总数")
    protected long totalCount;

    /**
     * 当前页码.
     */
    @ApiModelProperty(value = "当前页码")
    protected long currentPage;

    /**
     * 每页记录数.
     */
    @ApiModelProperty(value = "每页记录数")
    protected long pageSize;

    /**
     * The data.
     */
    @ApiModelProperty(value = "数据")
    private List<T> data;

    /**
     * 总页数
     */
    @ApiModelProperty(value = "总页数")
    protected long pages;
    /**
     * 是否第一页
     */
    @ApiModelProperty(value = "是否第一页", hidden = true)
    protected Boolean isFirstPage;

    /**
     * 是否最后一页
     */
    @ApiModelProperty(value = "是否最后一页", hidden = true)
    protected Boolean isLastPage;

    /**
     * 是否存在上一页
     */
    @ApiModelProperty(value = "是否存在上一页", hidden = true)
    protected Boolean hasPreviousPage;

    /**
     * 是否存在下一页
     */
    @ApiModelProperty(value = "是否存在下一页", hidden = true)
    protected Boolean hasNextPage;

    /**
     * 空分页对象
     */
    @SuppressWarnings("unchecked")
    public static final BasePaginationRsp EMPTY_PAGINATION = new BasePaginationRsp(0, 0,
            999, Collections.emptyList());

    public BasePaginationRsp() {
    }

    public BasePaginationRsp(long totalCount, long currentPage, long pageSize, List<T> data) {
        this.totalCount = totalCount;
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.data = data;
        this.pages = (totalCount / pageSize);
        if (this.totalCount % this.pageSize != 0) {
            this.pages += 1;
        }
    }

    public static BasePaginationRsp fromPageInfo(PageInfo pageInfo, List dtos) {
        BasePaginationRsp paginationRsp = new BasePaginationRsp();
        paginationRsp.setData(dtos);
        paginationRsp.setCurrentPage(pageInfo.getPageNum());
        paginationRsp.setPageSize(pageInfo.getPageSize());
        paginationRsp.setTotalCount(pageInfo.getTotal());
        paginationRsp.setPages(pageInfo.getPages());
        return paginationRsp;
    }
}
